from pycocotools.coco import COCO

dataDir = 'data/coco'
dataType = 'train2017'
# dataType='train2017'
annFile = '{}/annotations/instances_{}.json'.format(dataDir, dataType)

# initialize COCO api for instance annotations
coco = COCO(annFile)

# display COCO categories and supercategories
cats = coco.loadCats(coco.getCatIds())
cat_nms = [cat['name'] for cat in cats]
print('number of categories: ', len(cat_nms))
print('COCO categories: \n', cat_nms)

# 统计 各类的图片数量 和  标注框数量
print('类别	    图片数量	  标注框数量')
for cat_name in cat_nms:
    catId = coco.getCatIds(catNms=cat_name)  # 1~90
    imgId = coco.getImgIds(catIds=catId)  # 图片的id
    annId = coco.getAnnIds(catIds=catId)  # 标注框的id

    # print('-----------')
    # print('catId:',catId,'图片的id:',imgId,'标注框的id:',annId)

    print("{:<15} {:<6d}     {:<10d}".format(cat_name, len(imgId), len(annId)))